<template>
  <div class="http-node-card" ref="card">
    <!-- 删除蓝点锚点相关代码 -->
    <div class="header">
      <span class="icon"></span>
      <span class="title">Http请求节点</span>
    </div>
    <div class="desc">发送HTTP请求获取数据</div>
    <div class="section-title">请求配置</div>
    <div class="config-list">
      <div class="config-row">
        <label>请求方法：</label>
        <el-select v-model="localConfig.method" size="small" style="width: 100%;" @change="emitChange">
          <el-option label="GET" value="GET" />
          <el-option label="POST" value="POST" />
          <el-option label="PUT" value="PUT" />
          <el-option label="DELETE" value="DELETE" />
          <el-option label="PATCH" value="PATCH" />
        </el-select>
      </div>
      <div class="config-row">
        <label>请求URL：</label>
        <el-input v-model="localConfig.url" size="small" placeholder="请输入请求URL" @input="emitChange" />
      </div>
      <div class="config-row">
        <label>超时时间：</label>
        <el-input-number v-model="localConfig.timeout" :min="1000" :max="60000" :step="1000" size="small" style="width: 100%;" @change="emitChange" />
        <span style="margin-left: 8px; color: #888;">毫秒</span>
      </div>
    </div>
    <div class="section-title">请求头</div>
    <div class="headers-area">
      <div v-for="(header, idx) in localConfig.headers" :key="idx" class="header-row">
        <el-input v-model="header.key" placeholder="Header名称" size="small" style="width: 40%; margin-right: 8px;" @input="emitChange" />
        <el-input v-model="header.value" placeholder="Header值" size="small" style="width: 50%; margin-right: 8px;" @input="emitChange" />
        <el-button size="mini" type="danger" @click="removeHeader(idx)" @input="emitChange">删除</el-button>
      </div>
      <el-button size="mini" type="primary" @click="addHeader" style="margin-top: 8px;" @input="emitChange">添加Header</el-button>
    </div>
    <div class="section-title" v-if="localConfig.method !== 'GET'">请求体</div>
    <div class="body-area" v-if="localConfig.method !== 'GET'">
      <el-select v-model="localConfig.bodyType" size="small" style="width: 100%; margin-bottom: 8px;" @change="emitChange">
        <el-option label="JSON" value="json" />
        <el-option label="Form Data" value="form" />
        <el-option label="Raw Text" value="text" />
      </el-select>
      <el-input 
        v-model="localConfig.body" 
        type="textarea" 
        :rows="4"
        placeholder="请输入请求体内容"
        @input="emitChange"
      />
    </div>
    <div class="section-title">输出配置</div>
    <div class="config-list">
      <div class="config-row">
        <el-checkbox v-model="localConfig.saveToVariable" @change="emitChange">保存到变量</el-checkbox>
      </div>
      <div class="config-row" v-if="localConfig.saveToVariable">
        <label>变量名：</label>
        <el-input v-model="localConfig.outputVariable" size="small" placeholder="请输入输出变量名" @input="emitChange" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HttpRequestNodeCard',
  props: {
    config: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      localConfig: { ...this.config }
    }
  },
  watch: {
    config: {
      handler(newVal) {
        this.localConfig = { ...newVal }
      },
      deep: true
    }
  },
  methods: {
    addHeader() {
      this.localConfig.headers.push({ key: '', value: '' });
      this.emitChange();
    },
    removeHeader(idx) {
      this.localConfig.headers.splice(idx, 1);
      this.emitChange();
    },
    emitChange() {
      this.$emit('change', { ...this.localConfig })
    }
  }
};
</script>

<style scoped>
.http-node-card {
  width: 300px;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  box-shadow: 0 2px 8px 0 rgba(0,0,0,0.04);
  padding: 16px;
  position: relative;
}

.node-dot {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #2979ff;
  border: 2px solid #fff;
  box-shadow: 0 0 2px #2979ff;
  cursor: pointer;
  z-index: 10;
  transition: box-shadow 0.2s;
}

.node-dot--left-top {
  top: -6px;
  left: -6px;
}

.node-dot--right-top {
  top: -6px;
  right: -6px;
}

.node-dot:hover {
  box-shadow: 0 0 8px #2979ff;
}

.header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  position: relative;
}

.icon {
  width: 32px;
  height: 32px;
  background: #e1f5fe;
  border-radius: 50%;
  display: inline-block;
  margin-right: 8px;
  position: relative;
}

.icon::after {
  content: '🌐';
  display: block;
  width: 16px;
  height: 16px;
  font-size: 16px;
  position: absolute;
  left: 8px;
  top: 8px;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.desc {
  color: #888;
  font-size: 13px;
  margin-bottom: 8px;
}

.section-title {
  font-weight: bold;
  margin-bottom: 8px;
  margin-top: 12px;
}

.section-title:first-of-type {
  margin-top: 0;
}

.config-list {
  background: #fafbfc;
  border-radius: 6px;
  padding: 12px;
}

.config-row {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
}

.config-row:last-child {
  margin-bottom: 0;
}

.config-row label {
  display: block;
  margin-bottom: 4px;
  font-size: 14px;
  color: #333;
  width: 100%;
}

.headers-area {
  background: #fafbfc;
  border-radius: 6px;
  padding: 12px;
}

.header-row {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.body-area {
  background: #fafbfc;
  border-radius: 6px;
  padding: 12px;
}
</style> 